<?php

class Model_ServiceAnyemails extends Model_Service
{

	public function loginCheck($dpt_id, $dpt_account_login, $password="")
	{
		$service_info = $this->getServiceInfo($dpt_id);
			
		$dbconn = $this->getServiceConn($dpt_id);
		//Zend_Debug::dump($dbconn);
		//Zend_Debug::dump($service_info);
		if (isset($dbconn['all']))
		{
			if (strpos($dpt_account_login,'@anyemails.com')===false)
				$dpt_account_login .= '@anyemails.com';
				
			$select  = $dbconn['all']->select()->from('users');
			$select->where('email = ?', $dpt_account_login);
		
			$user = $dbconn['all']->fetchRow($select);
			//Zend_Debug::dump($user);
			
			$passwd = $password;
			
			if (!$user)
			{
				$rs['status'] = false;
				$rs['message'] = 'account not fund';
			}
			else
			{
			   	$status = $user['status'];
         		$passwd_stored = $user['password'];
         		
				 if ($status =='unusable')
		     	{
		        	$rs['status'] = false;
            		$rs['message'] = 'account not active';
		     	}
		     	elseif ($password && $passwd !=$passwd_stored)
		    	{
		        	$rs['status'] = false;
            		$rs['message'] = 'wrong password';
		     	}
		     	else
		     	{
		        	$rs['status'] = true;
            		$rs['dpt_account_id'] = $user['ID'];
           	 		$rs['dpt_account_login'] = $dpt_account_login;
           	 		
           	 		$rs['email'] = $user['email'];
           	 		$rs['password'] = $user['password'];
           	 		
            		$rs['login_area'] = 'member';
            
		     	}    
			}
		}
		else
		{
			$rs['status'] = false;
     		$rs['message'] = 'service connection error';
		}

		return $rs; // return OK if passed
	}

	
	public function createAccount($user_id, $dpt_id, $profile)
	{
		$password = Model_User::str_makerand(6,8); //create a random password for remote account
		
		$params=array();
		$params['password']=$password;

		if (is_array($profile)){
			foreach($profile as $key => $value){
				switch($key){
					case 'first_name':
					case 'last_name':
					case 'email':
					case 'company':
					case 'phone':
					case 'address':
					case 'city':
					case 'state':
					case 'zipcode':
					case 'country':
					case 'website':
						if (!empty($value)) $params[ $key ] = $value;
						break;
					default: break;
				}
			}
			
		}
		
		$remote_call = 'http://main.anyemails.com/server-rpc';
		$client = new Zend_XmlRpc_Client($remote_call);		
		$account_id = $client->call("YesupAcc.addUser",array($params));
		
		if ($account_id>0){
			$email_arr = explode('@',$profile['email']);
			$login_account = $email_arr[0].'_'.$account_id.'@anyemails.com';
			$dpt_login_arr=array();
			$dpt_login_arr['dpt_account_login'] = $login_account;
			$dpt_login_arr['login_area'] = 'member';
			$dpt_login_arr['email'] = $profile['email'];
			$dpt_login_arr['password'] = $password;
			$dpt_login_arr['admin_user_id'] = '';
			$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
			$dpt_account_id = $account_id;
			
			$model_client = new Model_UserClient();
			$result = $model_client->linkClientService($user_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
			if ($result){
				$ss = new Zend_Session_Namespace('service');
				$ss->link_result = 'success';
				$ss->link_dpt_id = $dpt_id;
				$ss->link_login_account = $login_account;
				$ss->link_dpt_account_id = $account_id;
				
				return true;
			}else 
				return false;			
		}else{
			return false;
		}
		
	}
	
	
}